<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/24
 * Time: 13:12
 */
class IndianaAction extends Action{
    public function getPeriodList($id=null,$state=null,$name=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $periodModule = M("module_mill_period");
        Session::switchCustomer($customerId);
        $condition['b.fromCustomerId']=array("eq",$customerId);
        isset($id)&&$id?$condition['a.id']=array("eq",$id):'';
        isset($state)&&$state? $condition['a.periodState']=array("eq",$state):'';
        isset($name)&&$name? $condition['b.name']=array("like","%".$name."%"):'';
        $list = $periodModule->alias("a")
            ->join("inner join wx_module_product b on a.pid = b.id")
            ->where($condition)
            ->field("a.*,b.name")
            ->order("a.id desc")
            ->limit($start,$limit)
            ->select();
        if ($list) {
            $count = $periodModule->alias("a")
                ->join("inner join wx_module_product b on a.pid = b.id")
                ->where($condition)
                ->field("a.*,b.name")
                ->order("a.id desc")
                ->count();
            Output::instance()->objArray($list,$count);
        } else {
            Output::instance()->resultCode();
        }
    }
    public function getDetailById($id,$name=null,$state=null,$fromName=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $millparModule = M("module_mill_par");
        Session::switchCustomer($customerId);
        $userInfo = M("user_weixin_info");
        $condition["a.perId"]=array("eq",$id);
        isset($state)&&$state!=null?$condition['a.parState']=array("eq",$state):'';
        isset($name)&&$name?$condition['b.nickname']=array("like","%".$name."%"):'';
        isset($fromName)&&$fromName?$condition['b.nickname']=array("like","%".$name."%"):'';

        $list = $millparModule->alias("a")
            ->join("inner join wx_module_mill_number d on a.id=d.parId")
            ->join("left join wx_".$customerId."_user_weixin_info b on a.openId=b.openid")
            ->join("inner join wx_module_product_order e on a.oid = e.id")
            ->where($condition)
            ->field("a.*,b.nickname,GROUP_CONCAT(d.number) as number,e.status")
            ->limit($start,$limit)
            ->order("a.createTime desc")
            ->group("a.id")
            ->select();
        if ($list) {
            $arr = $millparModule->alias("a")
                ->join("inner join wx_module_mill_number d on a.id=d.parId")
                ->join("left join wx_".$customerId."_user_weixin_info b on a.openId=b.openid")
                ->join("inner join wx_module_product_order e on a.oid = e.id")
                ->where($condition)
                ->field("a.id")
                ->group("a.id")
                ->select();
            $count = count($arr);
            Output::instance()->objArray($list,$count);
        } else {
            Output::instance()->resultCode();
        }
    }

    public function periodBackMoney($perId){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $periodModule = M("module_mill_period");
        $parModule = M("module_mill_par");
        Session::switchCustomer($customerId);
        $_GET["customerId"]=$customerId;
        $periodList = $periodModule->where("id=".$perId)->field("id,periodState")->find();
        if($periodList["periodState"]!=3){
            $periodModule->where("id=".$periodList["id"])->setField("periodState",5);
        }
        $parList = $parModule->alias("a")
            ->join("inner join wx_module_product_order c on a.oid = c.id")
            ->join("inner join wx_module_product b on a.pid = b.id")
            ->field("a.*,b.perMoney")
            ->where("perId=".$periodList["id"]." and (c.status='partake' or b.status = '".order_status_ok."' or b.status = '".order_status_delivered."')")
            ->select();

        foreach($parList as $k=>$val){
            $count = $parModule->where("perId is NULL and openId = '".$val["openId"]."' and pid = ".$val["pid"])->field("sum(count) as count")->find();
            $count = $count["count"]+$val["count"];
            $price = $count*$val["perMoney"];
            $oid = $val["oid"];
            $out=R('Product/orderRefund', array($oid,$price));
            $parInfo = $parModule->where("id=".$val["id"])->find();
            if($parInfo["parState"]!=2&&$parInfo["parState"]!=3){
                $parModule->where("id=".$val["id"])->setField("parState",4);
            }
        }
        if($periodList["periodState"]!=3) {
            $result = $periodModule->where("id=".$periodList["id"])->setField("periodState",4);
        }
        Output::instance()->resultcode();
    }
}